/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RegisterUser;

import java.sql.*;
import DBManager.DBConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Date;
/**
 *
 * @author Alex
 */
public class RegisterManager {
     public boolean addUser(Registration newUser)
    {
        boolean result = false;
        

        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            String password = newUser.getPassword();
          /*  
            
             MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(password.getBytes());
     
            byte byteData[] = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
             sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
        password=sb.toString();
            */
            
            
            //Construct SQL Query
            PreparedStatement pstmt =
                conn.prepareStatement("call register (?,?,?,?,?,?,?,?,?,?,?,?,?);");

            //append values for the parameters
            int i=1;

              pstmt.setString(i++, newUser.getUsername());
              pstmt.setString(i++, newUser.getPassword());
              pstmt.setString(i++, newUser.getFirstname());
              pstmt.setString(i++, newUser.getMiddlename());
              pstmt.setString(i++, newUser.getLastname());
              pstmt.setString(i++, newUser.getAddress());
              pstmt.setString(i++, newUser.getCity());
              pstmt.setString(i++, newUser.getPostal());
              pstmt.setString(i++, newUser.getEmail());   
              pstmt.setString(i++, newUser.getCreditname());
              pstmt.setString(i++, newUser.getCreditnumber());
              pstmt.setString(i++, newUser.getExpdate());
              pstmt.setString(i++, newUser.getCredittype());
              


            //execute SQL statement
            pstmt.executeUpdate();
            result = true;
            
          

        }
        catch(SQLException ex){ex.printStackTrace();}

        return result; 
        
    }
     
        public int addManager(Registration newManager)
    {
        int result = 0;
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
             ResultSet rs = null;
            String query="insert into customer (username,password,firstname,middlename,lastname,address,city,postal,email,creditname,creditnumber,expdate,credittype"
                    + " values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
          
            PreparedStatement pstmt =  conn.prepareStatement("insert into customer (username,password,firstname,middlename,lastname,address,city,postal,email,creditname,creditnumber,expdate,credittype)"
                    + " values(?,?,?,?,?,?,?,?,?,?,?,?,?)",PreparedStatement.RETURN_GENERATED_KEYS);
            
            //append values for the parameters
            int i = 1;
            pstmt.setString(i++, newManager.getUsername());
            pstmt.setString(i++, newManager.getPassword());
                    pstmt.setString(i++, newManager.getFirstname());
              pstmt.setString(i++, newManager.getMiddlename());
                pstmt.setString(i++, newManager.getLastname());
              
            pstmt.setString(i++, newManager.getAddress());
            pstmt.setString(i++, newManager.getCity());
            pstmt.setString(i++, newManager.getPostal());
    
          
          
            pstmt.setString(i++, newManager.getEmail());
            pstmt.setString(i++, newManager.getCreditname());
            pstmt.setString(i++, newManager.getCreditnumber());
            pstmt.setString(i++, newManager.getExpdate());
            pstmt.setString(i++, newManager.getCredittype());

            //execute SQL statement
            pstmt.executeUpdate();
     
            rs=pstmt.getGeneratedKeys();
            if(rs.next()){
                result=rs.getInt(1);
            }
            
         // result=1;
        }
        catch(SQLException ex){ex.printStackTrace();}
        
        return result;
    }
      
      
       public int Log(Registration newManager,int x)
    { Date newDate = new java.util.Date();
       int result=0;
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
            
            String query="insert into logs(CustomerId,message) values(?,?)";
                    String message=newManager.getUsername()+"\nDate Registered:"+newDate;
          
            PreparedStatement pstmt =  conn.prepareStatement("insert into logs(CustomerId,message) values(?,?)");
            
            //append values for the parameters
            int i = 1;
            pstmt.setInt(i++, x);
            pstmt.setString(i++, message);
           
            pstmt.executeUpdate();
result=1;
            //execute SQL statement
         
          
        }
        catch(SQLException ex){ex.printStackTrace();}
        
        return result;
    }
}
